package kr.hkit.powerjava.chapter24.submit3;

import java.util.InputMismatchException;
import java.util.Scanner;

public abstract class ServiceItem_Service<T> { // 메뉴랑 뭐 그런거

	protected Interface_Dao<T> dao;
	protected Scanner sc;

	public ServiceItem_Service(Interface_Dao<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
		prnMenu();
	}

	public abstract T inputItemInfo(boolean gubun);

	public abstract void prnSubMenu();

	protected void init() {
		dao.loadFile();
	}

	protected void close() {
		dao.saveFile();
	}

	public void prnMenu() {
		int res = 0;
//		T item = null; // 검색때문에 필요

		do {
			System.out.print("1.입력 2.삭제 3.수정 4.검색 5.리스트보기 6.종료");
			res = sc.nextInt();

			switch (res) {
				case 1:
					dao.insertItem(inputItemInfo(true));
					break;
				case 2:
					dao.prnItem(null);
					System.out.println("삭제할 번호를 선택하시오.");
					dao.deleteItem(inputItemInfo(false));
					break;
				case 3:
					dao.prnItem(null);
					System.out.println("수정할 정보 선택하시오.");
					dao.updateItem(inputItemInfo(true));
					break;
				case 4:
//					item = inputItemInfo(false);
					try {
//						item = dao.searchItem(item);
//						System.out.println(item); // 검색 정보 출력
						System.out.println(dao.searchItem(inputItemInfo(false)));
					} catch (InputMismatchException e) {
						System.out.println("검색 정보 없음");
					}
					break;
				case 5:
					prnSubMenu();
					break;
				default:
					close();
					res = 7;
					break;
			}
		} while (res <= 6);
	}
}
